//
//  XHHealthyScrollViewCell.m
//  HarmonizePub
//
//  Created by Haven on 2016/11/4.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "XHHealthyScrollViewCell.h"
#import "Config.h"
#import "UIView+extension.h"

@interface XHHealthyScrollViewCell()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, strong) NSArray *imageArray;

@end

@implementation XHHealthyScrollViewCell

- (NSArray *)imageArray
{
    if (_imageArray == nil) {
        _imageArray = [[NSArray alloc] init];
    }
    return _imageArray;
}

+ (NSString *)registerName {
    return @"XHHealthyScrollViewCell";
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.scrollView setFrame:CGRectMake(0, 0, XHScreenWidth, 135)];
    [self.scrollView setContentSize:CGSizeMake(XHScreenWidth * 5, 0)];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    _scrollView.delaysContentTouches = NO;
    self.pageControl.currentPage = 0;
    [self getScrollImage];

}

// 定时器
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

// 切换lable
- (void)nextImage
{
    CGPoint point = self.scrollView.contentOffset;
    point.x = point.x + self.scrollView.size.width;
    [self.scrollView setContentOffset: point animated:YES];
    


}


- (void)getScrollImage {
    NSString *urlString = [NSString stringWithFormat:@"%@", XHHomeScrollImagePath];
    NSDictionary *param = @{@"type" : XHType};
    //    XHLog(@"首页轮播图: path: %@\n\t参数: %@", urlString, param)
    [HTTPSTool GETRequestHidenHUDWithPath:urlString cookie:nil params:param progress:nil success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"首页轮播图: %@", JSON)
        [self setScrollImageWithImageArray:  JSON[@"data"][@"list"]];
    } failure:nil];
}

- (void)setScrollImageWithImageArray: (NSArray *)imageArray
{
    self.imageArray = imageArray;
    id lastObject = [self.imageArray lastObject];
    NSMutableArray *newImageArray = [NSMutableArray array];
    [newImageArray addObject: lastObject];
    [newImageArray addObjectsFromArray: self.imageArray];
    CGFloat imageH = self.scrollView.size.height;
    CGFloat imageW = self.scrollView.size.width;
    
    for(int i = 0; i < newImageArray.count; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView setFrame:CGRectMake(i * imageW, 0.0f, imageW, imageH)];
        [imageView downloadImageWithURLString:newImageArray[i][@"image"] placeholderString:@"ScrollImage"];
        [self.scrollView addSubview: imageView];
    }
    [self.scrollView setContentOffset:CGPointMake(XHScreenWidth, 0) animated:YES];
    
    [self addTimer];
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / self.bounds.size.width;

    if (self.scrollView.contentOffset.x == self.scrollView.width * self.imageArray.count) {
        
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated: NO];
            self.pageControl.currentPage = 0;
        
    }else{
        self.pageControl.currentPage = index;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer invalidate];
    self.timer = nil;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self addTimer];
}

@end
